package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.R;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ShowImgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<LocalMedia> mData;
    private List<File> fileList;
    private List<String> urls;
    private List<String> neImgList;
    private boolean isNet;
    private Intent intent;
    //Type
    private int TYPE_FOOT = 1001;
    private int TYPE_NET = 1001;

    public ShowImgAdapter(Context context, List<LocalMedia> data, List<String> urls) {
        mContext = context;
        mData = new ArrayList<>();
        this.urls = new ArrayList<>();
        fileList = new ArrayList<>();
        neImgList = new ArrayList<>();
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_image_show, parent, false);
        return new ViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            if (urls.get(position).contains("http")) {
                Glide.with(mContext).load(urls.get(position)).into(((ViewHolder) holder).img);
            } else {
                Bitmap bitmap = BitmapFactory.decodeFile(urls.get(position));
                ((ViewHolder) holder).img.setImageBitmap(bitmap);
            }

            ((ViewHolder) holder).img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openPhotoView(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return urls.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setDatas(List<String> urls) {
        this.urls = urls;
        isNet = false;
        this.mData = mData;
        notifyDataSetChanged();
    }

    public void setFileList(List<File> fileList) {
        this.fileList = fileList;
    }

    public void setNetImgist(List<String> netImgList) {
        this.neImgList = netImgList;
        this.urls.addAll(netImgList);
        isNet = true;
        notifyDataSetChanged();
    }

    public List<File> getFileList() {
        return this.fileList;
    }

    public List<String> getUrls() {
        return urls;
    }

    //预览
    private void openPhotoView(int position) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setClass(mContext, PhotoViewActivity.class);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) urls);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
        mContext.startActivity(intent);
    }

    /**
     * 正常内容的viewholder
     */
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
        }
    }
}